<?php

namespace handle\start;

use Workerman\Lib\Timer;

class Index
{
    public static function handle($worker)
    {
        Timer::add(10, function()use($worker){
            $time_now = time();
            if(!empty($worker->connections)){
                foreach($worker->connections as $connection) {
//                    dump($connection->uid);
//                    // 有可能该connection还没收到过消息，则lastMessageTime设置为当前时间
                    if (empty($connection->lastMessageTime)) {
                        $connection->lastMessageTime = $time_now;
                        continue;
                    }
                    // 上次通讯时间间隔大于心跳间隔，则认为客户端已经下线，关闭连接
                    if ( ($time_now - $connection->lastMessageTime) > WORKER_TIME_OUT) {
//                        $connection->send(sendData('reconnect'));//发送给客户端让它重新连接
                        $connection->close();
                    }
                }
            }
        });
    }
}